package com.kaoyaya.delivery.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;
import com.kaoyaya.delivery.activity.R;

/**
 * 这个示例演示如何在Fragment中添加布局视图
 * @date 2013-5-21
 * @author Goven
 * @email gxl3999@gmail.com
 */
@EFragment(R.layout.test_layout)
public class ExampleAddLayoutInFragment extends CenterFragment {

	@ViewById(R.id.vPager) ViewPager mViewPager;
	List<View> pagerList;
	LayoutInflater inflater;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		pagerList = new ArrayList<View>();
		return null;
	}
	
	@AfterViews
	protected void calledAfterViewInjection() {
		initView();
	}
	
	protected void initView() {
		// 必须调用这个方法，有些操作在父类CenterFragment中已经定义，
		// 父类的ViewPager对象需要与子类的ViewPager对象保持一致
		setViewPager(mViewPager);
		pagerList.add(inflater.inflate(R.layout.example_page, null));
		mViewPager.setAdapter(new MyAdapter());
		// 在父类中已经定义好了OnPageChangeListener监听器,这里仅仅设置即可
		// 如有必要,请实现下面的onPageSelected(int position)方法
		mViewPager.setOnPageChangeListener(pageChangeListener);
	}

	@Override
	protected void onPageSelected(int position) {
		
	}
	
	protected class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pagerList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			ViewPager v = ((ViewPager) container);
			v.removeView(pagerList.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ViewPager vp = ((ViewPager) container);
			vp.addView(pagerList.get(position));
			return pagerList.get(position);
		}
		
	}

}
